package com.fenghongzhang.lib_core.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.fenghongzhang.lib_core.R;
import com.google.android.material.resources.TextAppearance;

public class FentTextView extends AppCompatTextView {

    private static final String TAG = "FentTextView";
    public String string ;
    private Paint paint =new Paint(Paint.ANTI_ALIAS_FLAG);

    private int color;
    public FentTextView(Context context) {
        super(context,null);
    }

    //xml
    public FentTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);

    }

    public FentTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FentTextView);
        string = typedArray.getString(R.styleable.FentTextView_fengText);
        color = typedArray.getColor(R.styleable.FentTextView_fengColor, Color.BLACK);
        typedArray.close();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i(TAG, "dispatchTouchEvent: ");
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG, "onTouchEvent: ");
        return super.onTouchEvent(event);
        
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        switch (mode){
            case MeasureSpec.EXACTLY: // match_parent or 100dp
                break;

            case MeasureSpec.AT_MOST: // wrap_content
                size = 100;
                break;

            case MeasureSpec.UNSPECIFIED: //listview recyclerView
                break;

        }

        int modeH = MeasureSpec.getMode(heightMeasureSpec);
        int sizeH = MeasureSpec.getSize(heightMeasureSpec);
        switch (modeH){
            case MeasureSpec.EXACTLY: // match_parent or 100dp
                break;

            case MeasureSpec.AT_MOST: // wrap_content
                sizeH = 100;
                break;

            case MeasureSpec.UNSPECIFIED:
                break;

        }

        setMeasuredDimension(MeasureSpec.makeMeasureSpec(size,mode),MeasureSpec.makeMeasureSpec(sizeH,modeH));
    }



    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(color);
        canvas.drawText(string, 100, 100, paint);
    }
}
